package com.example.my_2048.been;

import android.graphics.Point;
import android.util.Log;

import com.example.my_2048.util.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tang on 16/8/18.
 */
public class Game {

    int arrsGameElement[][];
    List<Point> ltEmptyPoint;

    int iSumScore;

    public Game() {
        iSumScore = 0;
        arrsGameElement = new int[4][4];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                arrsGameElement[i][j] = 0;
            }
        }
        ltEmptyPoint = new ArrayList<>();
        Random();
    }

    public int[][] getArrsGameElement() {
        return arrsGameElement;
    }

    public void Reset() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                arrsGameElement[i][j] = 0;
            }
        }
        iSumScore=0;
        Random();
    }

    public void Random() {
        ltEmptyPoint.clear();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (arrsGameElement[i][j] <= 0) {
                    ltEmptyPoint.add(new Point(i, j));
                }
            }
        }
        int number = (int) (Math.random() * ltEmptyPoint.size());
        Log.i(Constants.TAG, "Random: " + number);
        Point ptRandomNumber = ltEmptyPoint.get(number);
        arrsGameElement[ptRandomNumber.x][ptRandomNumber.y] = Math.random() >= 0.5 ? 4 : 2;
    }

    public int getiSumScore() {
        return iSumScore;
    }

    public void setiSumScore(int iSumScore) {
        this.iSumScore = iSumScore;
    }


    public boolean touchRight() {
        boolean make = false;
        Log.i(Constants.TAG, "touchRight: ");
        for (int i = 0; i < 4; i++) {
            for (int j = 3; j >= 1; j--) {
                for (int z = j - 1; z >= 0; z--) {
                    if (arrsGameElement[i][z] == 0) {
                        continue;
                    } else {
                        if (arrsGameElement[i][j] == 0) {
                            arrsGameElement[i][j] = arrsGameElement[i][z];
                            arrsGameElement[i][z] = 0;
                            j++;
                            make = true;
                            break;
                        } else {
                            if (arrsGameElement[i][j] == arrsGameElement[i][z]) {
                                arrsGameElement[i][j] *= 2;
                                arrsGameElement[i][z] = 0;
                                iSumScore += arrsGameElement[i][j];
                                make = true;
                                break;
                            } else {
                                break;
                            }
                        }
                    }

                }
            }
        }
        return make;
    }

    public boolean touchUp() {
        Log.i(Constants.TAG, "touchUp: ");
        boolean make = false;
        for (int j = 0; j < 4; j++) {
            for (int i = 0; i < 3; i++) {
                for (int z = i + 1; z < 4; z++) {
                    if (arrsGameElement[i][j] == 0) {
                        if (arrsGameElement[z][j] > 0) {
                            arrsGameElement[i][j] = arrsGameElement[z][j];
                            arrsGameElement[z][j] = 0;
                            i--;
                            make = true;
                            break;
                        }
                    } else {
                        if (arrsGameElement[z][j] == arrsGameElement[i][j]) {
                            arrsGameElement[i][j] *= 2;
                            arrsGameElement[z][j] = 0;
                            iSumScore += arrsGameElement[i][j];

                            make = true;
                            break;
                        } else if (arrsGameElement[z][j] == 0) continue;
                        else break;
                    }
                }
            }
        }
        return make;
    }

    public boolean touchDown() {
        Log.i(Constants.TAG, "touchDown: ");
        boolean make = false;
        for (int j = 0; j < 4; j++) {
            for (int i = 3; i >= 1; i--) {
                for (int z = i - 1; z >= 0; z--) {
                    if (arrsGameElement[i][j] == 0) {
                        if (arrsGameElement[z][j] > 0) {
                            arrsGameElement[i][j] = arrsGameElement[z][j];
                            arrsGameElement[z][j] = 0;
                            i--;
                            make = true;
                            break;
                        }
                    } else {
                        if (arrsGameElement[z][j] == arrsGameElement[i][j]) {
                            arrsGameElement[i][j] *= 2;
                            arrsGameElement[z][j] = 0;
                            iSumScore += arrsGameElement[i][j];

                            make = true;
                            break;
                        } else if (arrsGameElement[z][j] == 0) continue;
                        else break;
                    }
                }
            }
        }
        return make;

    }

    public boolean touchLeft() {
        Log.i(Constants.TAG, "touchLeft(: ");

        boolean make = false;
        Log.i(Constants.TAG, "touchRight: ");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                for (int z = j + 1; z < 4; z++) {
                    if (arrsGameElement[i][z] == 0) {
                        continue;
                    } else {
                        if (arrsGameElement[i][j] == 0) {
                            arrsGameElement[i][j] = arrsGameElement[i][z];
                            arrsGameElement[i][z] = 0;
                            j++;
                            make = true;
                            break;
                        } else {
                            if (arrsGameElement[i][j] == arrsGameElement[i][z]) {
                                arrsGameElement[i][j] *= 2;
                                arrsGameElement[i][z] = 0;
                                iSumScore += arrsGameElement[i][j];
                                make = true;
                                break;
                            } else {
                                break;
                            }
                        }
                    }

                }
            }
        }
        return make;
    }

    // TODO: 16/8/19 判断游戏是否完成
    public boolean isGameEnd() {
        boolean make=true;
        int iTmp=iSumScore;
        int [][]arrayTmp=new int[4][4];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                arrayTmp[i][j]=arrsGameElement[i][j];
                Log.i(Constants.TAG, "isGameEnd: "+i+","+j+"="+arrayTmp[i][j]);
            }
        }
        Log.i(Constants.TAG, "isGameEnd:----------------- ");
        if(touchUp()||touchRight()||touchLeft()||touchDown())
        {
           make=false;
        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                arrsGameElement[i][j]=arrayTmp[i][j];
                Log.i(Constants.TAG, "isGameEnd: "+i+","+j+"="+arrayTmp[i][j]);
            }
        }
        iSumScore=iTmp;
        return make;

    }

    public boolean touchDirection(int iTouchDirection) {
        switch (iTouchDirection) {
            case Constants.DOWN:
                return touchDown();
            case Constants.LEFT:
                return touchLeft();
            case Constants.RIGHT:
                return touchRight();
            case Constants.UP:
                return touchUp();

        }
        return false;
    }
}
